package com.framework.dubbo.producer.file2html;

import cn.hutool.core.util.StrUtil;

import java.io.InputStream;

/**
 * 资源文件展示
 * @author xianggj
 * @Date 2021/6/8 12:52
 **/
public class SourcePreviewUtil extends AbstractFilePreviewTools {

    @Override
    public String convert2Html(String path) {
        return source2Html(path);
    }

    @Override
    public String convert2Html(InputStream in, String ext) {
        String filePath = writeToLocal(in, ext);
        return source2Html(filePath);
    }

    @Override
    public String convert2Str(InputStream in, String ext) {
        return "";
    }

    /**
     * 音频资源
     */
    private static final String AUDIO_HTML_FORMAT = "<audio controls><source src='%s' type='audio/mpeg'></audio>";

    /**
     * 视频资源
     */
    private static final String VIDEO_HTML_FORMAT = "<video src='%s' controls='controls'></video>";

    public static String source2Html(String path) {
        StringBuffer sb = new StringBuffer();
        sb.append("<html>");
        sb.append("<head>");
        sb.append("<meta name='viewport' content='width=device-width, minimum-scale=0.1'>");
        sb.append("</head>");
        sb.append("<body style='margin: 0px; background: #0e0e0e;'>");
        sb.append("<div border='0' width='100%' style='margin:auto;text-align:center;padding: 120px;'>");
        String ext = StrUtil.subAfter(path, ".", true);
        switch (FileType.enumOf(ext)) {
            case MP3:
                sb.append(String.format(AUDIO_HTML_FORMAT, path));
                break;
            case MP4:
            case AVI:
                sb.append(String.format(VIDEO_HTML_FORMAT, path));
                break;
            default:

        }
        sb.append("</div>");
        sb.append("</body>");
        sb.append("</html>");
        return sb.toString();
    }


}
